//202102104015柴亚楠
package cn.edu.sdjzu.xg.kcsj.dao;

import cn.edu.sdjzu.xg.kcsj.domain.User;
import cn.edu.sdjzu.xg.kcsj.domain.authority.Actor;
import cn.edu.sdjzu.xg.kcsj.service.ActorService;
import util.JdbcHelper;

import java.sql.*;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.TreeSet;

public final class UserDao {
    /**
     *单例模式
     *该类负责创建自己的对象，同时确保只有单个对象被创建。
     * 这个类提供了一种访问其唯一的对象的方式，可以直接访问，不需要实例化该类的对象。
     */
    private static UserDao userDao = new UserDao();
    private UserDao() {
    }
    public static UserDao getInstance() {
        if(userDao == null){
            userDao = new UserDao();
        }
        return userDao;
    }

    public Collection<User> findAll(Connection connection) throws SQLException {
        User user = null;
        String select_sql = "SELECT * FROM User";
        PreparedStatement pstmt = connection.prepareStatement(select_sql);
        ResultSet resultSet = pstmt.executeQuery();
        Collection<User> users = new TreeSet<User>();
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String username = resultSet.getString("username");
            String password = resultSet.getString("password");
            int shenfen = resultSet.getInt("shenfen");
            Actor actor = ActorService.getInstance().find(resultSet.getInt("actor_id"));
            Timestamp timestamp = resultSet.getTimestamp("lastLoginTime");
            LocalDateTime lastLoginTime = null;
            if (timestamp != null) {
                lastLoginTime = timestamp.toLocalDateTime();
            }
            user = new User(id, username, password,shenfen,lastLoginTime,actor);
            users.add(user);
        }
        return users;
    }
    public User find(Integer id,Connection connection) throws SQLException {
        User user = null;
        String select_sql = "SELECT * FROM User WHERE id = ?";
        PreparedStatement pstmt = connection.prepareStatement(select_sql);
        pstmt.setInt(1, id);
        ResultSet resultSet = pstmt.executeQuery();
        while (resultSet.next()) {
            String username = resultSet.getString("username");
            String password = resultSet.getString("password");
            int shenfen = resultSet.getInt("shenfen");
            Actor actor =ActorService.getInstance().find(resultSet.getInt("actor_id"));
            Timestamp timestamp = resultSet.getTimestamp("lastLoginTime");
            LocalDateTime lastLoginTime = null;
            if (timestamp != null) {
                lastLoginTime = timestamp.toLocalDateTime();
            }
            user = new User(id, username, password,shenfen,lastLoginTime,actor);
        }
        return user;
    }
    public boolean update(User user,Connection connection) throws SQLException {
        String update_sql = "UPDATE User SET username = ?,password = ?,shenfen=?" + "WHERE id = ?";
        PreparedStatement pstmt = connection.prepareStatement(update_sql);
        pstmt.setString(1, user.getUsername());
        pstmt.setString(2, user.getPassword());
        pstmt.setInt(3, user.getShenfen());
        pstmt.setInt(4, user.getId());
        if (pstmt.executeUpdate() == 0) {
            return false;
        } else {
            return true;
        }
    }

    public boolean delete(Integer id,Connection connection) throws SQLException {
        String delete_sql = "DELETE FROM User WHERE id = ?";
        PreparedStatement pstmt = connection.prepareStatement(delete_sql);
        pstmt.setInt(1, id);
        if (pstmt.executeUpdate() == 0) {
            return false;
        } else {
            return true;
        }
    }

    public Integer add(User user,Connection connection) throws SQLException {
        PreparedStatement pstmt = connection.prepareStatement(
                "INSERT INTO User (username,password,actor_id)" +
                        "VALUES(?,?,?)",Statement.RETURN_GENERATED_KEYS);
        pstmt.setString(1, user.getUsername());
        pstmt.setString(2, user.getPassword());
        pstmt.setInt(3,user.getActor().getId());
        int idOfAdded= 0;
        ResultSet resultSet = pstmt.getGeneratedKeys();
        if (resultSet.next()) {
            idOfAdded = resultSet.getInt(1);
        }
        return idOfAdded;
    }
    public User findByName (String username,Connection connection) throws SQLException {
        User desiredUser = null;

        PreparedStatement preparedStatement =
                connection.prepareStatement("SELECT * FROM user WHERE username=?");
        preparedStatement.setString(1, username);
        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next()) {
            int id = resultSet.getInt("id");
            String password = resultSet.getString("password");
            int shenfen  = resultSet.getInt("shenfen");
            Timestamp timestamp = resultSet.getTimestamp("lastLoginTime");
            LocalDateTime lastLoginTime = null;
            if (timestamp != null) {
                lastLoginTime = timestamp.toLocalDateTime();
            }
            desiredUser =
                    new User(id, username, password, shenfen,lastLoginTime);
        }
        JdbcHelper.close(resultSet,preparedStatement);
        return desiredUser;
    }

    public Actor login (User user,Connection connection) throws SQLException {
        Actor currentActor = null;
        String sql_login = "SELECT * FROM user WHERE username =? and password=? and shenfen=?";
        PreparedStatement pstmt_login =
                connection.prepareStatement(sql_login);
        pstmt_login.setString(1, user.getUsername());
        pstmt_login.setString(2, user.getPassword());
        pstmt_login.setInt(3, user.getShenfen());
        ResultSet resultSet_login = pstmt_login.executeQuery();
        if (resultSet_login.next()) {
            int actor_id = resultSet_login.getInt("actor_id");
            currentActor = ActorService.getInstance().find(actor_id);
        }
        return currentActor;
    }

}
